package com.gupaoedu.task.pattern.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class StarNewProxy implements InvocationHandler {
    private Object target;
    public Object getInstance(Object Stars){
        this.target = Stars;
        Class<?> clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(!runBefore(method)) {
            return null;
        }
        Object result =method.invoke(this.target, args);
        runAfter(method);
        return result;
    }


        private boolean runBefore(Method method){
            System.out.println("拦截到代理请求");
            if(method.getName().equals("dance")){
                System.out.println("明星脚受伤了，不能跳舞表演了");
                return false;
            }
            return true;
        }

        private void runAfter(Method method){
            System.out.println(method.getName() + "代理请求完毕");
        }


}
